<?php
	require 'login.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <?php
            include_once('generico.php');
            include_once('funzioni.php');
    
	generaHead();
	?>
    <body>
        <div id="container">
        <?php
			
            generaHeader();
            generaMenusx(isset($_GET['cat']));
            generaMenuLogin();
            echo '<div id="content">';
			if(isset($_GET['cat'])){
				$cat = cleanSimpleString($_GET['cat'], 20, '');
				$qry = "SELECT Count(Id) as Numero FROM Threads WHERE Categoria = '".$cat."';";
				$numeroThread = mysql_query($qry);
				$numeroThread = mysql_fetch_assoc($numeroThread);

				if(isset($_GET['pag']))
					$pag = cleanNumber($_GET['pag']);
				else 
					$pag = 0;

				$catFound=1;
				$qry="SELECT Titolo, CreatoDa as Autore, DATE_FORMAT(CreatoIl,'%H:%i(%d/%m/%Y)') as Data, Id FROM Threads
					  WHERE Categoria='$cat'
					  ORDER BY UltimoPost DESC LIMIT ".($pag*20).", 20 ";//20 threads per pagina			  
				
				$threads=mysql_query($qry) or die("Query non valida: " . mysql_error().$qry);

			}
			if(isset($threads) && mysql_num_rows($threads)!=0){
				echo '<h3>'.$cat.'</h3>
					<a href="index.php">Torna all\'elenco delle categorie</a>';

				echo '<div id="threads">';
				
				generaThreads($threads);
				echo '</div>';//threads

				//Frecce di navigazione pagine
				echo '<p class="navPagine">';
				if($pag > 0)
					echo '<a href="categoria.php?cat='.$cat.'&amp;pag='.($pag-1).'" title="Pagina precedente">&lt;&lt; indietro</a>';
				else
					echo '<span class="gray">&lt;&lt; indietro</span>';
				echo '&emsp;';
				if($pag!=0) 
					echo '<a href="categoria.php?cat='.$cat.'&amp;pag=0" title="Prima pagina">1</a>';
				else
					echo '<span class="gray" title="Pagina successiva">1</span>';

				echo ' . . . ';
				echo '<span title="Pagina corrente">'.($pag+1).'</span>';
				echo ' . . . ';

				if((($pag+1)*20) < $numeroThread['Numero']) {
					echo '<a href="categoria.php?cat='.$cat.'&amp;pag='.((int)(($numeroThread['Numero']-1)/20)).'" title="Ultima pagina">'.((int)(($numeroThread['Numero']-1)/20)+1).'</a>';					
					echo '&emsp;';
					echo '<a href="categoria.php?cat='.$cat.'&amp;pag='.($pag+1).'" title="Pagina successiva">avanti &gt;&gt;</a>';
				}
				else {
					echo '<span class="gray" title="Ultima pagina">'.((int)(($numeroThread['Numero']-1)/20)+1).'</span>';
					echo '&emsp;';	
					echo '<span class="gray" title="Pagina successiva">avanti &gt;&gt;</span>';
				}
				


				echo '</p>';
			}
			echo '</div>';//content
				
            
            generaFooter();
        ?>
        </div>
    </body>
</html>

